package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.event.IEventListener;
import com.eling.elcms.core.service.impl.ModelPersistentEvent;
import com.eling.elcms.core.service.impl.ModelPersistentEvent.Type;
import com.eling.elcms.travel.model.RoomPersonalInfo;
import com.eling.elcms.travel.service.IRoomPersonalInfoManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 人员房间信息新增时将personalinfo提取到主表
 * 若去掉signupregister与主表的直接关联，则不需要此监听器
 * @author zhoulin
 *
 */
@Service
public class RoomPersonalInfoListener implements IEventListener<ModelPersistentEvent> {

	@Autowired
	private IRoomPersonalInfoManager roomPersonalInfoManager;
	
	@Override
	public void doEvent(ModelPersistentEvent event) {
		if(event.getModel().getClass() == RoomPersonalInfo.class){
			if(event.getType() == Type.New){
				RoomPersonalInfo roomPersonalInfo = (RoomPersonalInfo) event.getModel();
				if(roomPersonalInfo.getNonMemberRegister() != null){
					roomPersonalInfo.setPersonalInfo(roomPersonalInfo.getNonMemberRegister().getSignUpRegister().getPersonalInfo());
				}
				if(roomPersonalInfo.getSignUpRegister() != null){
					roomPersonalInfo.setPersonalInfo(roomPersonalInfo.getSignUpRegister().getPersonalInfo());
				}
				roomPersonalInfoManager.save(roomPersonalInfo);
			}
		}
		
	}

}
